package net.sk.china.city.talk.web;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import net.sk.china.city.talk.response.FileResultResponse;
import net.sk.china.city.talk.service.UploadService;
import net.sk.china.common.response.CommonResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * +----------------------------------------------------------------------
 * // | Talk To [ 聊来改善地区金融服务环境，助力企业发展 ]
 * // +----------------------------------------------------------------------
 * // | Copyright (c) 2018-2024 聊来 All rights reserved.
 * // +----------------------------------------------------------------------
 * // | Licensed ( <a href="http://www.apache.org/licenses/LICENSE-2.0">apache</a> )
 * // +----------------------------------------------------------------------
 * // | @Author: 聊来 <18970881148@qq.com>
 * // +----------------------------------------------------------------------
 * // | DateTime: 2024/6/2 16:49
 * // +----------------------------------------------------------------------
 */

@RestController
@RequestMapping("upload/")
@Api(tags = "上传文件")
public class UploadController {

    private final UploadService uploadService;

    public UploadController(UploadService uploadService) {
        this.uploadService = uploadService;
    }

    /**
     * 图片上传
     */
    @ApiOperation(value = "图片上传")
    @RequestMapping(value = "image", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "model", value = "模块 用户user", dataTypeClass = String.class),
            @ApiImplicitParam(name = "pid", value = "分类ID 0编辑器,1商品图片,2拼团图片,3砍价图片,4秒杀图片,5文章图片,6组合数据图,7前台用户,8微信系列", allowableValues = "range[0,1,2,3,4,5,6,7,8]", dataTypeClass = Integer.class)
    })
    public CommonResult<FileResultResponse> image(MultipartFile multipart,
                                                  @RequestParam(value = "model") String model,
                                                  @RequestParam(value = "pid") Integer pid) throws Exception {
        return CommonResult.success(uploadService.imageUpload(multipart, model, pid));
    }

    /**
     * 文件上传
     */
    @ApiOperation(value = "文件上传")
    @RequestMapping(value = "file", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "model", value = "模块 用户user", dataTypeClass = String.class),
            @ApiImplicitParam(name = "pid", value = "分类ID 9企业授权协议书", dataTypeClass = Integer.class)
    })
    public CommonResult<FileResultResponse> file(MultipartFile multipart,
                                           @RequestParam(value = "model") String model,
                                           @RequestParam(value = "pid") Integer pid) throws Exception {
        return CommonResult.success(uploadService.fileUpload(multipart, model, pid));
    }
}
